﻿#include "MaterialTextureNodeView.h"

#include "MaterialGraph/Node/MaterialGraphNodeViewModel.h"
#include "MaterialTextureNodeViewModel.h"

#include "Widgets/TextWidget.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/Keyboard/KeyboardWidget.h"
#include "Widgets/Mouse/MouseWidget.h"

#include "Widgets/Positioners/VerticalPositionerWidget.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Asset/EditorDefaultPackage.h"
#include "Widgets/EditorAssetSelectorWidget.h"

void RMaterialTextureNodeView::BindViewModel(TSharedObjectPtr<RMaterialGraphNodeViewModel> InModel)
{
    Super::BindViewModel(InModel);

    TextureViewModel = InModel;
}

void RMaterialTextureNodeView::RegenerateContentWidget()
{
    int64_t Line= TextureViewModel->IsParameter()?2:1;

    auto RGBAOutput= CreateSimpleOutputPinWidget(RootWidget, RootWidget, 0, Line);
    Line++;

    auto RGBOutput= CreateSimpleOutputPinWidget(RootWidget, RootWidget, 1, Line);
    Line++;

    auto ROutput= CreateSimpleOutputPinWidget(RootWidget, RootWidget, 2, Line);
    Line++;

    auto GOutput= CreateSimpleOutputPinWidget(RootWidget, RootWidget, 3, Line);
    Line++;

    auto BOutput= CreateSimpleOutputPinWidget(RootWidget, RootWidget, 4, Line);
    Line++;

    auto AOutput= CreateSimpleOutputPinWidget(RootWidget, RootWidget, 5, Line);
    Line++;

    auto SelectTextureLine= CreateLine(RootWidget, RootWidget, Line, 1);
    Line++;
    {
        auto Lable= NewWidget<RTextWidget>(SelectTextureLine);
        Lable->Text= U"图片:";
        Lable->X= BaseViewModel->LineHorizontalPadding;
        Lable->Width=40;

        auto ComboBoxBackground= NewWidget<RRectangleWidget>(SelectTextureLine);
        ComboBoxBackground->Anchor.VerticalFill(SelectTextureLine);
        ComboBoxBackground->Anchor.RightReference =SelectTextureLine;
        ComboBoxBackground->Anchor.RightOffset=BaseViewModel->LineHorizontalPadding;
        ComboBoxBackground->X= BaseViewModel->LineHorizontalPadding+40;
        ComboBoxBackground->FillColor= REditor::Get()->CurrentTheme->MaterialEditableBackground;
        ComboBoxBackground->Radius=BaseViewModel->BackgroundRadius;

        auto ComboBoxText= NewWidget<RTextWidget>(SelectTextureLine);
        ComboBoxText->Anchor.Fill(ComboBoxBackground);
        ComboBoxText->Text= [this]()->CString
        {
            return TextureViewModel->GetTextureDisplayName();
        };

        auto ComboBoxIcon=NewWidget<RImageWidget>(SelectTextureLine);
        ComboBoxIcon->Anchor.RightReference=ComboBoxBackground;
        ComboBoxIcon->Anchor.VerticalCenterReference=SelectTextureLine;
        ComboBoxIcon->Width=20;
        ComboBoxIcon->Height=20;
        ComboBoxIcon->Image=
            REditorDefaultPackage::Get()->GetIcon(U"CodiconChevronDown");

        auto PopupKeyboard= NewWidget<RKeyboardWidget>(SelectTextureLine);
        RKeyboardWidgetWeakPtr WeakPopupKeyboard= PopupKeyboard;

        auto ComboBoxMouse= NewWidget<RMouseWidget>(SelectTextureLine);
        ComboBoxMouse->Anchor.Fill(SelectTextureLine);
        ComboBoxMouse->AutoFocusTarget= PopupKeyboard;

        auto ComboBoxPopup= NewWidget<RWidget>(RootWidget);
        PopupKeyboard->Anchor.Fill(ComboBoxPopup);
        ComboBoxPopup->Height= TextureViewModel->GetPopupMenuHeight();
        ComboBoxPopup->Width= TextureViewModel->GetPopupMenuWidth();
        ComboBoxPopup->Anchor.BottomReference=ComboBoxText;
        ComboBoxPopup->Anchor.BottomReferenceEdge=EVerticalWidgetEdge::Top;
        ComboBoxPopup->Anchor.LeftReference=ComboBoxText;
        ComboBoxPopup->Visible=[WeakPopupKeyboard](){
            return WeakPopupKeyboard->IsFocused();
        };

        auto PopupBackground= NewWidget<RRectangleWidget>(ComboBoxPopup);
        PopupBackground->Anchor.Fill(ComboBoxPopup);
        PopupBackground->FillColor= REditor::Get()->CurrentTheme->BackgroundColor;
        PopupBackground->Radius=4;

        auto Selector=NewWidget<REditorAssetSelectorWidget>(ComboBoxPopup);
        Selector->Anchor.Fill(ComboBoxPopup);
        Selector->OnAssetSelected.Bind([this,PopupKeyboard](TSharedObjectPtr<RObject> SelectedTexture)
        {                
            TextureViewModel->SelectTexture(SelectedTexture);
            PopupKeyboard->Unfocus();
        });
        Selector->SetAssetType(EEditorSelectableAssetType::Texture);
    }

    auto DisplayTexture= CreateLine(RootWidget, RootWidget, Line, 4); 
    Line+=4;
    {
        auto Background= NewWidget<RRectangleWidget>(DisplayTexture);
        RRectangleWidgetWeakPtr WeakBackground= Background;
        Background->FillColor= SColor::Black;
        Background->Anchor.VerticalFill(DisplayTexture);
        Background->Anchor.RightReference =DisplayTexture;
        Background->Anchor.RightOffset=BaseViewModel->LineHorizontalPadding;
        Background->X= BaseViewModel->LineHorizontalPadding;

        auto Image= NewWidget<RImageWidget>(DisplayTexture);
        Image->Anchor.Fill(Background);
        Image->Image= [this]()->TSharedObjectPtr<RTexture2D>
        {
            return TextureViewModel->GetTexture();
        };
    }

    auto UVInput= CreateSimpleInputPinWidget(RootWidget, RootWidget, 0, Line);
    Line++;

}